$(document).ready(function() {
	if($("#processType").val()=="update"){
		$('#vaUserId').attr('readonly', true);
	}

});

jQuery.fn.dataTableExt.oApi.fnPagingInfo = function ( oSettings )
		{
			return {
				"iStart":         oSettings._iDisplayStart,
				"iEnd":           oSettings.fnDisplayEnd(),
				"iLength":        oSettings._iDisplayLength,
				"iTotal":         oSettings.fnRecordsTotal(),
				"iFilteredTotal": oSettings.fnRecordsDisplay(),
				"iPage":          oSettings._iDisplayLength === -1 ?
					0 : Math.ceil( oSettings._iDisplayStart / oSettings._iDisplayLength ),
				"iTotalPages":    oSettings._iDisplayLength === -1 ?
					0 : Math.ceil( oSettings.fnRecordsDisplay() / oSettings._iDisplayLength )
			};
		};

	
	function dataTable(data) {
		var oLanguage = data.split(",");

		$("#siteRoleData").dataTable( {
			"bJQueryUI": true,
			"sPaginationType": "full_numbers",
			"oLanguage": {
	            "oPaginate": {
	                          "sFirst": oLanguage[0],// ปุ่มกลับมาหน้าแรก
	                          "sLast": oLanguage[1],//ปุ่มไปหน้าสุดท้าย
	                          "sNext": oLanguage[2], //ปุ่มหน้าถัดไป
	                          "sPrevious": oLanguage[3] // ปุ่ม กลับ
	                        	  },
	             "sLengthMenu": oLanguage[4],  
	             "sZeroRecords": oLanguage[5], 
	             "sInfo": oLanguage[6],
	             "sEmptyTable": oLanguage[7],
	             "sLoadingRecords": oLanguage[8],
	             "sProcessing": oLanguage[9],
	             "sInfoEmpty": oLanguage[10],
	             "sInfoFiltered": oLanguage[11]
	        },
	        "bProcessing": true,
//	        "bServerSide": true,
//	        "sort": "position",
	        "bStateSave": false,
	        "iDisplayLength": 10,
	        "iDisplayStart": 0,
	        "fnDrawCallback": function () {   
	        },
	        "sAjaxSource": "user/siteRoleDataTables",
	        "aoColumns": [
				{ "mData": null,sClass: "center", sWidth: '5%',"mRender": function ( data, type, full ) {
					
						var vaChecked = "";
						if($("#vaSiteAndRoleId").val()!=""){
							var vaUserMapping = $("#vaSiteAndRoleId").val().split(",");

							for(var i=0; i<vaUserMapping.length; i++){
								if(vaUserMapping[i] == (full.vaSiteId+"|"+full.vaRoleId)){
									vaChecked = "checked";
									break;
								}
							}

						}
						
						return "<label class=\"label-checkbox\">"+
						"<input type=\"checkbox\" name=\"check_list\" id=\"check_list\" value=\""+full.vaSiteId+"|"+full.vaRoleId+"\" "+vaChecked+" Onclick=\"CheckList(document.dataTable.check_list)\">"+
						"<span class=\"custom-checkbox info\"></span>"+
						"</label>";
				},"bSortable": false, "bSearchable": false},
				{ "mData": "vaSiteId",sClass: "center", sWidth: '5%' },
				{ "mData": "vaSiteName",sClass: "left", sWidth: '20%'  },
				{ "mData": "vaRoleId",sClass: "center", sWidth: '5%'},
				{ "mData": "vaRoleName",sClass: "left", sWidth: '20%' },
	        ]} 
		);
		
		$('#chk-all').click(function()	{
			if($(this).is(':checked'))	{
				$('#responsiveTable').find('.chk-row').each(function()	{
					$(this).prop('checked', true);
					$(this).parent().parent().parent().addClass('selected');
				});
			}
			else	{
				$('#responsiveTable').find('.chk-row').each(function()	{
					$(this).prop('checked' , false);
					$(this).parent().parent().parent().removeClass('selected');
				});
			}
		});
	}
	
	function CheckInitialCheckBox(){
		
	}
	
	function CheckAll(){
		var chk = document.dataTable.check_list;
		var paSiteAndRoleId = [];
		for (i = 0; i < chk.length; i++){
			if($('#checkall').is(':checked')){
				chk[i].checked = true ;
				paSiteAndRoleId.push(chk[i].value);
			}else{
				paSiteAndRoleId = "";
				chk[i].checked = false ;
			}
		}
		
		$("#vaSiteAndRoleId").val("");
		$("#vaSiteAndRoleId").val(paSiteAndRoleId);
	}
	
	function CheckList(chk){

		var paSiteAndRoleId = [];
		var n = 0;
		for (i = 0; i < chk.length; i++){
			if(chk[i].checked == true){
				paSiteAndRoleId.push(chk[i].value);
				n++;
			}
		}

		if(n==chk.length){
			$("#checkall").prop('checked', true);
		}else{
			$("#checkall").prop('checked', false);
		}

		$("#vaSiteAndRoleId").val(paSiteAndRoleId);
	}
	
	function submitForm(){
		$('#userLogin')[0].submit();
	}
	
	function clearForm(){
		$("#processType").val("insert");
		$('#vaUserId').attr('readonly', false);
		$('#userLogin')[0].reset();

		document.getElementById("vaStatus").selectedIndex = "0";
		var chk = document.getElementsByTagName("input");
		for (i = 0; i < chk.length; i++){
			var el = chk[i];
			  if (el.type == 'checkbox' && el.checked) {
				  chk[i].checked = false ;
			  }else{
				  chk[i].value = "";
			  }
				
		}
	}
	